home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / agrep / checkfile.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  2KB  |  105 lines

  1. /*
  2.  *  checkfile.c
  3.  *    takes a file descriptor and checks to see if a file is a regular
  4.  *    ascii file
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include <fcntl.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <errno.h>
  14.  
  15. #include "checkfile.h"
  16.  
  17. #define MAXLINE 512
  18.  
  19. extern char Progname[];
  20. extern int errno;
  21.  
  22. unsigned char ibuf[MAXLINE];
  23.  
  24. /**************************************************************************
  25. *
  26. *    check_file
  27. *       input:  filename or path (null-terminated character string)
  28. *       returns: int (0 if file is a regular file, non-0 if not)
  29. *
  30. *    uses stat(2) to see if a file is a regular file.
  31. *
  32. ***************************************************************************/
  33.  
  34. int check_file(fname)
  35. char *fname;
  36.  
  37. {
  38. struct stat buf;
  39. int ftype;
  40.  
  41.  
  42.   if (stat(fname, &buf) != 0) {
  43.     if (errno == ENOENT)
  44.       return NOSUCHFILE;
  45.     else
  46.       return STATFAILED;  
  47.     } else {
  48. /*
  49.       if (S_ISREG(buf.st_mode)) {
  50.         if ((ftype = samplefile(fname)) == ISASCIIFILE) {
  51.           return ISASCIIFILE;
  52.         } else if (ftype == ISBINARYFILE) {
  53.           return ISBINARYFILE;
  54.         } else if (ftype == OPENFAILED) {
  55.           return OPENFAILED;
  56.         }
  57.       }
  58.       if (S_ISDIR(buf.st_mode)) {
  59.         return ISDIRECTORY;
  60.       }
  61.       if (S_ISBLK(buf.st_mode)) {
  62.         return ISBLOCKFILE;
  63.       }
  64.       if (S_ISSOCK(buf.st_mode)) {
  65.         return ISSOCKET;
  66.       }
  67. */
  68.     }
  69. }
  70.  
  71. /***************************************************************************
  72. *
  73. *  samplefile
  74. *    reads in the first part of a file, and checks to see that it is
  75. *    all ascii.
  76. *
  77. ***************************************************************************/
  78. /*
  79. int samplefile(fname)
  80. char *fname;
  81. {
  82. char *p;
  83. int numread;
  84. int fd;
  85.  
  86.   if ((fd = open(fname, O_RDONLY)) == -1) {
  87.     fprintf(stderr, "open failed on filename %s\n", fname);
  88.     return OPENFAILED;
  89.   }
  90.   if (numread = read(fd, ibuf, MAXLINE)) {
  91.    close(fd);
  92.    p = ibuf;
  93.     while (isascii(*p++) && --numread);
  94.     if (!numread) {
  95.       return(ISASCIIFILE);
  96.     } else {
  97.       return(ISBINARYFILE);
  98.     }
  99.   } else {
  100.     close(fd);
  101.     return(ISASCIIFILE);
  102.   }
  103. }
  104. */
  105.